<%
   issues = @snapshot.measure('violations')
   blocker_issues = @snapshot.measure('blocker_violations')
   critical_issues = @snapshot.measure('critical_violations')
   major_issues = @snapshot.measure('major_violations')
   minor_issues = @snapshot.measure('minor_violations')
   info_issues = @snapshot.measure('info_violations')
   new_issues = @snapshot.measure('new_violations')

   technical_debt = @snapshot.measure('sqale_index')
   new_technical_debt = @snapshot.measure('new_technical_debt')
%>
<div class="widget-row">

  <% if technical_debt %>
    <div class="widget-span widget-span-3">
      <div class="widget-measure-container">
        <div class="widget-measure widget-measure-main">
          <span class="widget-label"><%= message('widget.rules.technical_debt') -%></span>
          <a href="<%= url_for_drilldown('sqale_index') -%>" class="widget-link link-<%= widget.key %>-debt">
            <%= format_measure(technical_debt) -%>
          </a>
          <%= dashboard_configuration.selected_period? ? format_variation(technical_debt) : trend_icon(technical_debt) -%>
        </div>
        <div class="widget-measure-delta">
          <%
             if @dashboard_configuration.selected_period?
               technical_debt_variation = variation_value(technical_debt)
               new_technical_debt_variation = variation_value(new_technical_debt)
               estimated_cleared_technical_debt = (new_technical_debt_variation - technical_debt_variation).to_i if technical_debt_variation && new_technical_debt_variation
          %>
            <% if new_technical_debt_variation && new_technical_debt_variation > 0 %>
              <p class="small">
                <%= message('widget.rules.added') %>
                <a href="<%= url_for_drilldown('new_technical_debt', :period => @dashboard_configuration.period_index) -%>"
                   class="varw widget-link widget-link-red link-<%= widget.key %>-new-debt"><%= format_variation(new_technical_debt, :style => 'none', :default => '-') -%></a>
              </p>
            <% end %>
            <% if estimated_cleared_technical_debt && estimated_cleared_technical_debt > 0 %>
              <p class="small">
                <%= message('widget.rules.removed') %>
                <span class="varb"><%= Internal.i18n.formatLongDuration(estimated_cleared_technical_debt, 'SHORT') -%></span>
              </p>
            <% end %>
          <% end %>
        </div>
      </div>
    </div>
  <% end %>

  <div class="widget-span widget-span-3">
    <div class="widget-measure-container">
      <div class="widget-measure widget-measure-main">
        <span class="widget-label"><%= message('widget.rules.issues') -%></span>
        <span class="nowrap">
          <span class="link-<%= widget.key %>-issues">
            <%= format_measure(issues, :url => url_for(:controller => 'drilldown', :action => 'issues', :id => @project.key)) -%>
          </span>
          <%= dashboard_configuration.selected_period? ? format_variation(issues) : trend_icon(issues) -%>
        </span>
      </div>
      <div class="widget-measure-delta">
        <%
           if @dashboard_configuration.selected_period?
             issues_variation = variation_value(issues)
             new_issues_variation = variation_value(new_issues)
             estimated_cleared_issues = (new_issues_variation - issues_variation).to_i if issues_variation && new_issues_variation
        %>
          <% if new_issues_variation && new_issues_variation > 0 %>
                    <p class="small">
                      <%= message('widget.rules.added') %>
                      <a href="<%= url_for(:controller => 'drilldown', :action => 'issues', :id => @project.key, :period => dashboard_configuration.period_index) -%>"
                         class="varw widget-link widget-link-red link-<%= widget.key %>-new-issues"><%= format_variation(new_issues, :style => 'none', :default => '-') -%></a>
                    </p>
          <% end %>
          <% if estimated_cleared_issues && estimated_cleared_issues > 0 %>
                    <p class="small">
                      <%= message('widget.rules.removed') %>
                      <span class="varb"><%= number_with_precision(estimated_cleared_issues, :precision => 0) -%></span>
                    </p>
          <% end %>
        <% end %>
      </div>
    </div>
  </div>

  <div class="widget-span widget-span-6">
    <%
       values = [blocker_issues, critical_issues, major_issues, minor_issues, info_issues]
       if dashboard_configuration.selected_period?
         values = values.map { |m| m ? (m.variation(dashboard_configuration.period_index)||0) : 0 }
       else
         values = values.map { |m| m ? (m.value||0) : 0 }
       end
       max = values.map { |val| val.abs }.max
    %>
    <table class="data widget-barchar">
      <tr>
        <td class="nowrap">
          <i class="icon-severity-blocker"></i>
          <%= message('blocker') -%>
        </td>
        <td class="thin right nowrap">
          <%= link_to format_measure(blocker_issues), {:controller => 'drilldown', :action => 'issues', :id => @project.key, :severity => 'BLOCKER'}, :class => 'widget-link drilldown_BLOCKER' -%>
        </td>
        <td class="thin">
          <%= dashboard_configuration.selected_period? ? format_variation(blocker_issues) : trend_icon(blocker_issues, :empty => true) -%>
        </td>
        <td class="nowrap">
          <% if max > 0 %>
            <% if dashboard_configuration.selected_period? %>
              <%= barchart(:width => 35, :percent => (values[0]<0 ? (100 * values[0] / max).to_i : 0), :color => '#85bb43') %>
              <%= barchart(:width => 35, :percent => (values[0]>0 ? (100 * values[0] / max).to_i : 0), :color => '#d4333f') %>
            <% else %>
              <%= barchart(:width => 70, :percent => (100 * values[0] / max).to_i) %>
            <% end %>
          <% end %>
        </td>
      </tr>
      <tr>
        <td class="nowrap">
          <i class="icon-severity-critical"></i>
          <%= message('critical') -%>
        </td>
        <td class="thin right nowrap">
          <%= link_to format_measure(critical_issues), {:controller => 'drilldown', :action => 'issues', :id => @project.key, :severity => 'CRITICAL'}, :class => 'widget-link drilldown_CRITICAL' -%>
        </td>
        <td class="thin">
          <%= dashboard_configuration.selected_period? ? format_variation(critical_issues) : trend_icon(critical_issues, :empty => true) -%>
        </td>
        <td class="nowrap">
          <% if max > 0 %>
            <% if dashboard_configuration.selected_period? %>
              <%= barchart(:width => 35, :percent => (values[1]<0 ? (100 * values[1] / max).to_i : 0), :color => '#85bb43') %>
              <%= barchart(:width => 35, :percent => (values[1]>0 ? (100 * values[1] / max).to_i : 0), :color => '#d4333f') %>
            <% else %>
              <%= barchart(:width => 70, :percent => (100 * values[1] / max).to_i) %>
            <% end %>
          <% end %>
        </td>
      </tr>
      <tr>
        <td class="nowrap">
          <i class="icon-severity-major"></i>
          <%= message('major') -%>
        </td>
        <td class="thin right nowrap">
          <%= link_to format_measure(major_issues), {:controller => 'drilldown', :action => 'issues', :id => @project.key, :severity => 'MAJOR'}, :class => 'widget-link drilldown_MAJOR' -%>
        </td>
        <td class="thin">
          <%= dashboard_configuration.selected_period? ? format_variation(major_issues) : trend_icon(major_issues, :empty => true) -%>
        </td>
        <td class="nowrap">
          <% if max > 0 %>
            <% if dashboard_configuration.selected_period? %>
              <%= barchart(:width => 35, :percent => (values[2]<0 ? (100 * values[2] / max).to_i : 0), :color => '#85bb43') %>
              <%= barchart(:width => 35, :percent => (values[2]>0 ? (100 * values[2] / max).to_i : 0), :color => '#d4333f') %>
            <% else %>
              <%= barchart(:width => 70, :percent => (100 * values[2] / max).to_i) %>
            <% end %>
          <% end %>
        </td>
      </tr>
      <tr>
        <td class="nowrap">
          <i class="icon-severity-minor"></i>
          <%= message('minor') -%>
        </td>
        <td class="thin right nowrap">
          <%= link_to format_measure(minor_issues), {:controller => 'drilldown', :action => 'issues', :id => @project.key, :severity => 'MINOR'}, :class => 'widget-link drilldown_MINOR' -%>
        </td>
        <td class="thin">
          <%= dashboard_configuration.selected_period? ? format_variation(minor_issues) : trend_icon(minor_issues, :empty => true) -%>
        </td>
        <td class="nowrap">
          <% if max > 0 %>
            <% if dashboard_configuration.selected_period? %>
              <%= barchart(:width => 35, :percent => (values[3]<0 ? (100 * values[3] / max).to_i : 0), :color => '#85bb43') %>
              <%= barchart(:width => 35, :percent => (values[3]>0 ? (100 * values[3] / max).to_i : 0), :color => '#d4333f') %>
            <% else %>
              <%= barchart(:width => 70, :percent => (100 * values[3] / max).to_i) %>
            <% end %>
          <% end %>
        </td>
      </tr>
      <tr>
        <td class="nowrap">
          <i class="icon-severity-info"></i>
          <%= message('info') -%>
        </td>
        <td class="thin right nowrap">
          <%= link_to format_measure(info_issues), {:controller => 'drilldown', :action => 'issues', :id => @project.key, :severity => 'INFO'}, :class => 'widget-link drilldown_INFO' -%>
        </td>
        <td class="thin">
          <%= dashboard_configuration.selected_period? ? format_variation(info_issues) : trend_icon(info_issues, :empty => true) -%>
        </td>
        <td class="nowrap">
          <% if max > 0 %>
            <% if dashboard_configuration.selected_period? %>
              <%= barchart(:width => 35, :percent => (values[4]<0 ? (100 * values[4] / max).to_i : 0), :color => '#85bb43') %>
              <%= barchart(:width => 35, :percent => (values[4]>0 ? (100 * values[4] / max).to_i : 0), :color => '#d4333f') %>
            <% else %>
              <%= barchart(:width => 70, :percent => (100 * values[4] / max).to_i) %>
            <% end %>
          <% end %>
        </td>
      </tr>
    </table>
  </div>
</div>
